import time

import requests
from bs4 import BeautifulSoup


def get_html(url):
    '''
    采集html
    :param url:
    :return:
    '''
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
        "sec-ch-ua": "\"Google Chrome\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }
    cookies = {
        "nc_rela": "1",
        "novelrelative": "1217177",
        "readline": "1",
        "fontFamily": "1",
        "fontsize": "16",
        "vip_img_width": "3",
        "font_Color": "666666",
        "host4chongzhi": "https%3a%2f%2fb.faloo.com%2f1217177_1.html",
        "Hm_lvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744513557",
        "HMACCOUNT": "6FC63036CE9F6FEF",
        "KeenFire": "UMID=37161038&UserID=18483678377&Pwd=6b0c368ea7e62c4f239e7ad16decdf94&Identity=web45760.5295572508&PhotoID=0&NickName=%e7%a8%8b%e5%ba%8f%e5%91%98Mars",
        "UU12345678": "uuc=133889880564592627728089052",
        "comment_reply": "0",
        "favorates28": "1217177%2C15",
        "autobuychapters28": "1217177%2C15",
        "bgcolor": "%23FFFFFE",
        "curr_url": "https%3A//b.faloo.com/html_1217_1217177/",
        "Hm_lpvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744514485"
    }
    response = requests.get(url, headers=headers, cookies=cookies)

    html = response.text
    parse_html(html)


def parse_html(html):
    '''
    解析html
    :param html:
    :return:
    '''
    soup = BeautifulSoup(html, "html.parser")
    detail_url_list = soup.select(".c_con_list .c_con_li_detail_p")

    for detail_url in detail_url_list:
        detail_url = detail_url.a.attrs["href"]
        detail = "https:" + detail_url
        get_detail_html(detail)


def get_detail_html(url):
    print(url)
    '''
    采集详情页html
    :param url:
    :return:
    '''

    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "none",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
        "sec-ch-ua": "\"Google Chrome\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }
    cookies = {
        "nc_rela": "1",
        "novelrelative": "1217177",
        "readline": "1",
        "fontFamily": "1",
        "fontsize": "16",
        "vip_img_width": "3",
        "font_Color": "666666",
        "host4chongzhi": "https%3a%2f%2fb.faloo.com%2f1217177_1.html",
        "Hm_lvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744513557",
        "HMACCOUNT": "6FC63036CE9F6FEF",
        "KeenFire": "UMID=37161038&UserID=18483678377&Pwd=6b0c368ea7e62c4f239e7ad16decdf94&Identity=web45760.5295572508&PhotoID=0&NickName=%e7%a8%8b%e5%ba%8f%e5%91%98Mars",
        "UU12345678": "uuc=133889880564592627728089052",
        "C3VK": "989182",
        "favorates28": "1217177%2C21",
        "autobuychapters28": "1217177%2C21",
        "bgcolor": "%23FFFFFE",
        "comment_reply": "0",
        "curr_url": "https%3A//b.faloo.com/1217177_1.html",
        "Hm_lpvt_6d308f6626f6d0864b6bb4f348f2b5e5": "1744515646"
    }
    time.sleep(1)
    proxy = {
        "http": "http://127.0.0.1:22307",
        "https": "http://127.0.0.1:22307"
    }

    # 快代理
    response = requests.get(url, headers=headers, cookies=cookies, proxies=proxy)

    html = response.text
    parse_detail_html(html)


def parse_detail_html(html):
    '''
    解析详情页html
    :param html:
    :return:
    '''
    soup = BeautifulSoup(html, "html.parser")
    content = soup.find('div', class_='noveContent').text
    print(content)


if __name__ == '__main__':
    get_html("https://b.faloo.com/html_1217_1217177/")
